package com.jindi.appuser.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: wuwei
 * @date: 2018/3/21 17:01
 */

public class TimeUtils {
    public static long getLiveTime(Date time, String appId) {
        if (time == null) return 0L;
        long now = time.getTime();
        long timeToLive = 0L;
        switch (appId) {
            case "1":
            case "6":
                timeToLive = 50 * 5 * 60 * 1000;
                //保留五十期，每隔5分钟一期，23:59:59-09:00:00之间不开
                if (now >= getEndTime(23, 59, 59) - timeToLive) {
                    timeToLive += 9 * 60 * 60 * 1000;
                }
                break;
            case "2":
            case "5":
                timeToLive = 100 * 5 * 60 * 1000;
                if (now >= getEndTime(26, 0, 0) - timeToLive) {
                    timeToLive += 9 * 60 * 60 * 1000;
                }
                break;
            case "3":
                timeToLive = 50 * 5 * 60 * 1000;
                if (now >= getEndTime(28, 0, 0) - timeToLive) {
                    timeToLive += 9 * 60 * 60 * 1000;
                }
                break;
            case "4":
                timeToLive = 50 * 5 * 60 * 1000;
                if (now >= getEndTime(22, 0, 0) - timeToLive) {
                    timeToLive += 9 * 60 * 60 * 1000;
                }
                break;
        }
        return timeToLive;
    }

    /**
     * 返回当天的结束时间
     *
     * @return
     */
    public static long getEndTime(int hour, int minute, int second) {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hour, minute, second);
        return cal.getTimeInMillis();
    }

}
